// pages/homestay/homestay.js
const $api = require('../utils/api').API
const app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        /* 民宿列表数据 */
        homeLists: [],
        color_default: '#FDC63D',
        color_host: 'black',
        color_distance: 'black',
        color_price: 'black',
        //配置的域名。
        baseURL: 'https://j53421893f.zicp.fun', //公网映射后的地址
        /* 当前位置 */
        coordinates1: 109.50,
        coordinates2: 30.30,
        /* 民宿标题 */
        name: '',
        type: null,
    },
    /* 搜索民俗 */
    changeSortByName() {
        this.relreshList(this.data.type);        
    },
    /* 民宿详情 */
    homeDetail(e) {
        console.log(e.currentTarget.dataset.item); 
        wx.navigateTo({
            url: '../homestay_detail/homestay_detail?id='+ e.currentTarget.dataset.item.id,
        })

    },
    // 改变颜色
    changeColor(type) {
        console.log(type);
        let color = ['color_default', 'color_host', 'color_distance', 'color_price'];
        this.setData({
            color_default: 'black',
            color_host: 'black',
            color_distance: 'black',
            color_price: 'black',
        })
        this.setData({
            [type]: '#FDC63D'
        })
    },
    // 刷新民俗列表
    relreshList(type) {
        this.data.type = type
        var data = {  
            "coordinates1": 109.50, //经度
            "coordinates2": 30.30 //纬度
        }
        if (this.data.name != null && this.data.name != '') {
            data.name = this.data.name
        }
        if (type == 1) {

        } else if (type == 2) {
            data.scale = 1
        } else if (type == 3) {
            data.coordinates = 1
        } else if (type == 4) {
            data.lowPrice = 1
        }

        if(null != this.data.name && '' != this.data.name && ' ' != this.data.name){
            data.name = this.data.name
        }

        //请求列表接口
        $api.getHomestayList(data).then(res => {
            if (res.data.code !== 200) {
                wx.showToast({ //弹出框
                    title: '查询民宿列表失败！',
                    icon: 'error',
                    duration: 2000
                })
            } else {
                // wx.showToast({
                //   title: '查询民宿列表成功！',
                //   icon: 'success',
                //   duration: 2000
                // });
                this.data.homeLists = res.data.rows;
                // console.log(res.data);
                for (let i = 0; i < this.data.homeLists.length; i++) {
                    let data2 = {
                        "outerId": this.data.homeLists[i].id,
                        "moduleId": 3,
                        "innerId": 1,
                    }
                    $api.selectFiles(data2).then(res2 => {
                        if (res2.data.code == 200) {
                            if (res2.data.rows.length > 0)
                                this.data.homeLists[i].src = this.data.baseURL + res2.data.rows[0].filePath;
                            this.setData({
                                homeLists: this.data.homeLists
                            })
                        } else {}
                    })
                }
                this.setData({
                    homeLists: this.data.homeLists
                })
                console.log(this.data.homeLists);

            }
        })
    },
    /* 改变排序方式 */
    changeSort(e) {
        this.changeColor(e.target.dataset.type);
        /* 刷新民宿列表 */
        if(e.target.dataset.type == 'color_default') this.relreshList(1);
        if(e.target.dataset.type == 'color_host') this.relreshList(2);
        if(e.target.dataset.type == 'color_distance') this.relreshList(3);
        if(e.target.dataset.type == 'color_price') this.relreshList(4);
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        var data = {
            "id": 1, //封面
            "moduleId": 1, //封面
            "coordinates1": 109.50, //经度
            "coordinates2": 30.30 //纬度
        }
        // if(null != options.name){ 
        //     data.name = options.name
        //     this.data.name  = options.name
        // }
        //请求列表接口
        $api.getHomestayList(data).then(res => {
            if (res.data.code !== 200) {
                wx.showToast({ //弹出框
                    title: '查询民宿列表失败！',
                    icon: 'error',
                    duration: 2000
                })
            } else {
                // wx.showToast({
                //   title: '查询民宿列表成功！',
                //   icon: 'success',
                //   duration: 2000
                // });
                this.data.homeLists = res.data.rows;
                // console.log(res.data);
                for (let i = 0; i < this.data.homeLists.length; i++) {
                    let data2 = {
                        "outerId": this.data.homeLists[i].id,
                        "moduleId": 3,
                        "innerId": 1,
                    }
                    $api.selectFiles(data2).then(res2 => {
                        if (res2.data.code == 200) {
                            if (res2.data.rows.length > 0)
                                this.data.homeLists[i].src = this.data.baseURL + res2.data.rows[0].filePath;
                            this.setData({
                                homeLists: this.data.homeLists
                            })
                        } else {}
                    })
                }
                this.setData({
                    homeLists: this.data.homeLists
                })
                console.log(this.data.homeLists);

            }
        })
        /* 获取民宿图片 */
        let data1 = {
            "moduleId": 5,
            "innerId": 1
        }
        $api.selectFiles(data1).then(res => {
            if (res.data.code !== 200) {
                wx.showToast({ //弹出框
                    title: '查询民宿图片失败！',
                    icon: 'error',
                    duration: 2000
                })
            } else {
                // wx.showToast({
                //   title: '查询民宿图片成功！',
                //   icon: 'success',
                //   duration: 2000
                // });
                /* 保存图片 */
                //console.log(res.data);
                for (let i = 0; i < res.data.rows.length - 1; i++) {
                    //   this.data.homeLists[i].address = this.data.baseURL + res.data.rows[i].filePath;
                }
                this.setData({
                    homeLists: this.data.homeLists
                });
                console.log(this.data.homeLists);
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow(options) {
        // console.log(options);
        if(null != app.globalData.storeName){
            this.data.name = app.globalData.storeName
            this.relreshList()
            app.globalData.storeName = null
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})